classdef SpectrumModule < EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule
    %SPECTRUMMODULE Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
    end
    
    methods
        function obj = SpectrumModule(server, name)
            %SPECTRUMMODULE Construct an instance of this class
            %   Detailed explanation goes here
            obj@EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule(server, name);
        end
        
        function obj = initialize(obj, varargin)
            p=inputParser;
            p.addParameter('tSync', 0.2, @(x) x>=0);
            p.parse(varargin{:});
            
            initialize@EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule(obj);
            obj.setModuleSettingValue(ziSettingsDAQModule.device, obj.devices{1});
            obj.setModuleSettingValue(ziSettingsDAQModule.enable, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.endless, 0);
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_rows, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.historylength, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.preview, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.spectrum_enable, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.spectrum_frequencyspan, 1e3);
            obj.setModuleSettingValue(ziSettingsDAQModule.type, 0);
            obj.sync(p.Results.tSync);
            obj.domain = 'frequency';
            obj.setUpdateMode('replace');
        end
        
        %%
        function obj = setFrequencyGrid(obj, nCol, varargin)
            L = log4m.getLogger;
            
            p=inputParser;
            p.addRequired('nCol', @(x) x > 2 );
            p.addParameter('fSpan', 1e3, @(x) x>0);
            p.addParameter('mode',  ziEnumGridMode.Exact, @ziEnumGridMode.test);
            p.addParameter('tSync', 0.2,                  @(x) x>=0);
            p.parse(nCol, varargin{:});
            
            grid_cols = 2^ceil(log2(nCol));
            if grid_cols ~= nCol
                L.info('SpectrumModule::setFrequencyGrid', sprintf('Grid column number is adjused from [%d] to [%d]', nCol, grid_cols));
            end
            obj.setModuleSettingValue(ziSettingsDAQModule.spectrum_frequencyspan, 2.0*p.Results.fSpan);
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_cols, grid_cols);
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_mode, p.Results.mode);
            obj.sync(p.Results.tSync);
        end
    end
end

